package com.spring.forum.service;

import com.spring.forum.mapper.BlogMapper;
import com.spring.forum.mapper.UserInfoMapper;
import com.spring.forum.model.BlogInfo;
import com.spring.forum.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogMapper blogMapper;


    /**
     * 根据用户名查找用户
     */
    public UserInfo getUserByName(String username) {
        return userInfoMapper.selectUserInfoByName(username);
    }


    /**
     * 根据当前登录的用户id拿取用户信息
     */
    public UserInfo getUserById(Integer userId) {
        return userInfoMapper.selectUserInfoById(userId);
    }

    /**
     * 根据博客id获取用户信息
     */
    public UserInfo getUserInfoByBlogId(Integer blogId) {
        // 1. 通过博客id获取用户id
        BlogInfo blogInfo = blogMapper.selectBlogById(blogId);
        if (blogInfo == null) {
            // 无该 blogId 对应的文章，返回 null
            return null;
        }

        // 2. 通过用户 id 拿到用户信息
        return userInfoMapper.selectUserInfoById(blogInfo.getUserId());
    }
}
